<?php


class User_m extends CI_Model {

    var $details;

    function validate_user( $username, $password ) {
        // Build a query to retrieve the user's details
        // based on the received username and password
        //$this->db->from('zwop_user');
        //$this->db->where('username',$username );
        //$this->db->where( 'password', sha1($password) );
        $login = $this->db->get_where('zwop_user',array('username'=>$username,'password'=>sha1($password)))->result();

        // The results of the query are stored in $login.
        // If a value exists, then the user account exists and is validated
        if ( is_array($login) && count($login) == 1 ) {
            // Set the users details into the $details property of this class
            $this->details = $login[0];
            // Call set_session to set the user's session vars via CodeIgniter
            $this->set_session();
            return true;
        }

        return false;
    }

    function set_session() {
        // session->set_userdata is a CodeIgniter function that
        // stores data in CodeIgniter's session storage.  Some of the values are built in
        // to CodeIgniter, others are added.  See CodeIgniter's documentation for details.
        $this->session->set_userdata( array(
                'id'=>$this->details->id,
                'username'=> $this->details->username,
                'isadmin'=>$this->details->isadmin,
                'isLoggedIn'=>true
            )
        );
    }

    function  create_new_user( $userData ) {
      $data['username'] = $userData['username'];
      $data['isadmin'] = (int) $userData['isadmin'];
      $data['password'] = sha1($userData['password']);

      return $this->db->insert('zwop_user',$data);
    }


}
